<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 商品
 *
 * @icon fa fa-circle-o
 */
class Goodsselect extends Backend
{

    /**
     * Goodsselect模型对象
     * @var \app\admin\model\Goodsselect
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Goodsselect;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {

        $param =$this->request->param();
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {

            $this->assign('agent_id',$param['agent_id']);
            return $this->view->fetch();
        }

        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            ->with(['category'])
            ->where($where)
            ->where(['deletetime'=>0])
            ->order($sort, $order)
            ->paginate($limit);

        $goods=Db::table('zwa_service_agent_goods_relation')->where(['service_agent_id'=>$param['agent_id']])->column('goods_id');
        $list->each(function ($item)use($goods){
            if(in_array($item['id'],$goods)){
                $item['check_value'] =  1;
            }else{
                $item['check_value'] =  0;
            }
            return $item;
        });

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


    /**添加代理产品
     * @throws \think\Exception
     * @author: zhaoyu
     * @Time: 2024/1/27 14:26
     */
    public  function   agentGoods(){
        $param =$this->request->param();
        if(isset($param['agent_id']) && empty($param['agent_id'])){
            $this->error('服务商请选择');
        }
        if(isset($param['goods_id']) && empty($param['goods_id'])){
            $this->error('商品请选择');
        }

        $goodsArr= json_decode($param['goods_id'],true);
//        var_dump($goodsArr);die;
        $idsArr=[];

        $result = false;
        Db::startTrans();
        try {
            foreach ($goodsArr as $k=>$v){
                $ids=[
                    'service_agent_id'=>$param['agent_id'],
                    'goods_id'=>$v,
                ];
                Db::table('zwa_service_agent_goods_relation')->where($ids)->delete();
                $row=Db::table('zwa_service_agent_goods_relation')->insert($ids);
            }
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success();
    }

    public  function  deleteAgentGoods(){
        $param =$this->request->param();

        if(isset($param['agent_id']) && empty($param['agent_id'])){
            $this->error('服务商请选择');
        }
        if(isset($param['goods_id']) && empty($param['goods_id'])){
            $this->error('商品请选择');
        }
        $goodsArr= json_decode($param['goods_id'],true);
        $ids=[
            'service_agent_id'=>$param['agent_id'],
            'goods_id'=>$goodsArr[0],
        ];
        Db::table('zwa_service_agent_goods_relation')->where($ids)->delete();

        $this->success();
    }

    public function indexs()
    {

//        $param =$this->request->param();
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {

//            $this->assign('agent_id',$param['agent_id']);
            return $this->view->fetch();
        }

        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            ->with(['category'])
            ->where($where)
            ->where(['deletetime'=>0])
            ->order($sort, $order)
            ->paginate($limit);

//        $goods=Db::table('zwa_service_agent_goods_relation')->where(['service_agent_id'=>$param['agent_id']])->column('goods_id');
//        $list->each(function ($item)use($goods){
//            if(in_array($item['id'],$goods)){
//                $item['check_value'] =  1;
//            }else{
//                $item['check_value'] =  0;
//            }
//            return $item;
//        });

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }



}
